export default {
    namespaced: true,
    state: {
        source:'wangyiyun',
        // 是否加载中
        loading: false,
        // 歌单
        songList: [],
        // 歌曲总数
        totalCount: 0,
        // 歌单缓存
        songListCache: {},
        // 当前页数
        page: 0,
        // 关键词
        keyword: ''
    },
    mutations: {
        setState(state, option) {
            for (const key in option) {
                state[key] = option[key]
            }
        }
    },
    actions: {
        // 获取搜索结果
        search(state, { keyword }) {
            // 如果又keyword，可能是重新搜索
            if (keyword) {
                state.state.keyword = keyword
                state.state.page = 0
            }
            // 缓存id
            let cacheId = state.state.keyword + '_' + state.state.page


            return new Promise((resolve, reject) => {
                // 查看缓存
                if (state.state.songListCache[cacheId]) {
                    return state.state.songList = state.state.songListCache[cacheId]
                }
                
                fetch(`http://111.67.201.30:3000/cloudsearch?keywords= ${state.state.keyword}&limit=30&offset=${30 * state.state.page}`).then(_ => _.json()).then(result => {
                    if (result.code === 200) {
                        // 记录搜索总数
                        state.state.totalCount = Number(result.result.songCount)
                        // 记录搜索歌曲的信息
                        let songListData = result.result.songs.map(item => {
                            let timeStamp = parseInt(item.dt / 1000)
                            let second = timeStamp % 60
                            second = second < 10 ? '0' + second : second
                            let songTimeMinutes = `${parseInt(timeStamp / 60)}:${second}`
                            return {
                                rid: item.id,
                                name: item.name,
                                artist: item.ar[0].name,
                                album: item.al.name,
                                songTimeMinutes,
                                imgsrc: item.al.picUrl,
                                source:state.state.source
                            }
                        })
                        state.state.songList = songListData
                        // 缓存歌曲信息
                        state.state.songListCache[cacheId] = songListData
                        resolve()
                    }
                }).catch(err => reject(err))
            })
        },

        // 获取播放地址
        getMusicUrl(state, id) {
            return new Promise((resolve, reject) => {
                fetch(`http://111.67.201.30:3000/song/url?id=${id}`)
                    .then((_) => _.json())
                    .then((res) => {
                        if (res.code === 200) {
                            resolve({ code: 200, url: res.data[0].url })
                        }
                    })
                    .catch(err => reject(err))
            })
        },

        // 获取歌词
        getLyric(state, id) {
            return new Promise((resolve, reject) => {
                fetch(`http://111.67.201.30:3000/lyric?id=${id}`).then(_ => _.json())
                    .then(res => resolve(res.lrc.lyric))
                    .catch(err => reject(err))
            })
        },

        // 监听换页
        changePage(state, page) {
            // 更新页数
            state.state.page = page - 1
            state.dispatch('search', {})
        }

    }
}
